package com.xin.spring.project.util;

import java.sql.Connection;

/**
 *  连接
 *
 * @author  xinyue
 */
public class ConnectionUtil {

    private ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    private ConnectionUtil(){}
    public static ConnectionUtil connectionUtil = new ConnectionUtil();
    public static ConnectionUtil getInstance(){
        return connectionUtil;
    }

    /**
     * 获取当前线程的连接
     *
     * @return 结果
     * @throws Exception 异常
     */
    public Connection getCurrentThreadConn() throws Exception {
        Connection con = threadLocal.get();
        if (con == null) {
            con = DruidUtil.getInstance().getConnection();
            threadLocal.set(con);
        }
        return con;
    }
}
